module executor.message.client.TransferMsgExecutor;

import msgtrans;
import message.Constants;
import cache.UserStatusManager;
import hunt.logging;
import google.protobuf;
import message.MsgDefine;
import cache.ChatIdMap;
import client.GateServerClient;
import client.MsgServerClient;
import std.array;
import std.conv : to;
import client.LogicServerClient;

@TransportClient(LogicClientName)
class TransferMsgExecutor : AbstractExecutor!(TransferMsgExecutor){

  @MessageId(MESSAGE.CMD_MESSAGE_TRANSFER)
  void sendMsg(TransportContext ctx, MessageBuffer buffer)
  {
      auto req = new Msg();
      buffer.data.fromProtobuf!Msg(req);

      logInfof("msgserver receive red packet msg", req.localId);

      if (req.type == MsgType.NOTICE_RED_ENVELOPE) //拼接红包被领取的通知消息
      {
        if (UserStatusManager.getInstance().getUserStatus(req.localId))
        {
          TransportSession[ulong] contexts = GateServerClient.getInstance.getAllClient();
          foreach(TransportSession c ; contexts)
          {
            logInfo("222222");
            c.send(new MessageBuffer(MESSAGE.CMD_MESSAGE_TRANSFER,req.toProtobuf.array));
          }
        }else
        {
          MessageTransportClient logicClient = LogicServerClient.getInstance().getForwardClient();
          logicClient.send(new MessageBuffer(MESSAGE.CMD_PUT_UNRECEIVE,req.toProtobuf.array));
            logInfo("33333");
        }

        return ;
      }

      // 以下主要转发发送红包的消息
      string [string] clients = ChatIdMap.getInstance().getGroupInfo(req.chatId);

      logInfo("----%s  %s %d",clients, clients.length);

      foreach(string clientId ; clients)
      {
        MessageTransportClient client = MsgServerClient.getInstance.getForwardClient(clientId);
        logInfo("id: %s",clientId);
        logInfo("size: %d",MsgServerClient.getInstance.getSize);
        req.localId = clientId;
        if (client is null) //hash到本机
        {
          logInfo("clientId local: %s",clientId );
          if (UserStatusManager.getInstance().getUserStatus(clientId))
          {
            TransportSession[ulong] contexts = GateServerClient.getInstance.getAllClient();
            foreach(TransportSession c ; contexts)
            {
              logInfo("644444444 %s", clientId);
              c.send(new MessageBuffer(MESSAGE.CMD_MESSAGE_TRANSFER,req.toProtobuf.array));
            }
          }
          else
          {
            logInfo("700000 %s", clientId);
            MessageTransportClient logicClient = LogicServerClient.getInstance().getForwardClient();
            logicClient.send(new MessageBuffer(MESSAGE.CMD_PUT_UNRECEIVE,req.toProtobuf.array));
          }
        }else
        {
          //buffer.id = MESSAGE.CMD_MESSAGE_TRANSFER_HASH;
          logInfo("clientId other server: %s",clientId );
          client.send(new MessageBuffer(MESSAGE.CMD_MESSAGE_TRANSFER_HASH,req.toProtobuf.array));
        }

        logInfo("811111111 %s", clientId);

        MessageTransportClient logicClient = LogicServerClient.getInstance().getForwardClient();
        logicClient.send(new MessageBuffer(MESSAGE.CMD_PUT_HISTORY_MSG,req.toProtobuf.array));
      }
  }
}

